import { INestApplication } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { knife4jSetup } from 'nestjs-knife4j';
import { isDev } from '@/config';
/**
 * 配置Swagger文档
 * @param app - NestJS应用实例
 *
 */
export const setupSwagger = (app: INestApplication,) => {
    if (!isDev) {
        return; // 非开发环境不启用Swagger
    }
    
    // Swagger配置选项
    const swaggerOptions = new DocumentBuilder()
        .setTitle('接口文档')
        .setDescription('api接口文档')
        .setVersion('1.0.0')
        .addBearerAuth(
            {
                type: 'http',
                scheme: 'bearer',
                bearerFormat: 'JWT',
                name: 'Authorization',
                description: 'Enter your JWT token in this format - Bearer token',
                in: 'header',
            },
        )
        .build();
    
    // 创建Swagger文档
    const document = SwaggerModule.createDocument(app, swaggerOptions);
    // 项目依赖当前文档功能，最好不要改变当前地址
    // 生产环境使用 nginx 可以将当前文档地址 屏蔽外部访问
    // 设置Swagger文档访问路径
    SwaggerModule.setup(`api`, app, document);
    
    // 配置knife4j
    knife4jSetup(app, {
        urls: [
            {
                name: '2.X版本',
                url: `/api-json`,
                swaggerVersion: '3.0',
                location: `/api-json`,
            },
        ],
    });
    
    console.log('Swagger文档已启用，访问地址: http://localhost:端口/api');
};
